<?php
/**
 * @author ykm 2015-12-15
 * @describe 申通请求电子面单类
 */
class erpapi_logistics_matrix_sf_request_electron extends erpapi_logistics_request_electron
{

    public $node_type = 'sf';
    public $to_node = '1588336732';
    public $shop_name = '顺丰官方电子面单';
    
    public function bufferRequest($sdf){
        return $this->directNum;
    }

    public function directRequest($sdf){
        $this->primaryBn = $sdf['primary_bn'];
        $delivery = $sdf['delivery'];
        $shopInfo = $sdf['shop'];
        $totalAmount = $sdf['total_amount'];
        $dlyCorp = $sdf['dly_corp'];
        $deliveryItems = $sdf['delivery_item'];
        $cargos = $this->getCargos($deliveryItems);
        $jAddress = $shopInfo['address_detail'] ? $shopInfo['province'] . $shopInfo['city'] . $shopInfo['area'] . $shopInfo['address_detail'] : '_SYSTEM';
        $dAddress = $delivery['ship_addr'] ? $delivery['ship_province'] . $delivery['ship_city'] . $delivery['ship_district'] . $delivery['ship_addr'] : '_SYSTEM';
        $delivery_bn = $delivery['delivery_bn'] ;
        $params = array(
            'orderid' => $delivery_bn,
            'express_type' => $this->__channelObj->channel['logistics_code'],
            'j_company' => $shopInfo['shop_name'],
            'j_contact' => $shopInfo['default_sender'] ? $shopInfo['default_sender'] : '_SYSTEM',
            'j_tel' => $shopInfo['mobile'] ? $shopInfo['mobile'] : ($shopInfo['tel'] ? $shopInfo['tel'] : '_SYSTEM'),
            'j_province' => $shopInfo['province'],
            'j_city' => $this->isMunicipality($shopInfo['province']) ? $shopInfo['province'] : $shopInfo['city'],
            'j_address' => $this->charFilter($jAddress),
            'd_company' => $this->charFilter($delivery['ship_name']),
            'd_contact' => $this->charFilter($delivery['ship_name']),
            'd_tel' => $delivery['ship_mobile'] ? $delivery['ship_mobile'] : $delivery['ship_tel'],
            'd_province' => $delivery['ship_province'],
            'd_city' => $this->isMunicipality($delivery['ship_province']) ? $delivery['ship_province'] : $delivery['ship_city'],
            'd_address' => $this->charFilter($dAddress),
            'parcel_quantity' => 1,
            'cargo' => $this->charFilter(htmlspecialchars($cargos['cargo'])),
            'cargo_total_weight' => $delivery['net_weight'] ? sprintf("%.2f", $delivery['net_weight']/1000) : '',
        );
        //货到付款
        if ($delivery['is_cod'] == 'true') {
            list ($sysAccount, $passWord, $pay_method, $custid) = explode('|||', $this->__channelObj->channel['shop_id']);
            $params['sf_cod'] = 'COD';
            $params['sf_cod_value'] = $totalAmount;
            $params['sf_cod_value1'] = $custid;
        }
        if ($dlyCorp['protect'] == 'true') {
            $params['sf_insure'] = 'INSURE';
            $params['sf_insure_value'] = max($totalAmount * $dlyCorp['protect_rate'], $dlyCorp['minprice']);
        }
        $back = $this->requestCall(STORE_SF_ORDERSERVICE, $params);
        return $this->backToResult($back, $delivery);
    }

    public function backToResult($back, $delivery){
        if ($back['res'] == '8016') {

            $back = $this->searchRequest($delivery);
        }
        $data = empty($back['data']) ? '' : json_decode($back['data'], true);
        if(empty($data)) {
            return false;
        }
        $result = array();
        $result[] = array(
            'succ' => $data['mailno'] ? true : false,
            'msg' => '',
            'delivery_id' => $delivery['delivery_id'],
            'delivery_bn' => $delivery['delivery_bn'],
            'logi_no' => $data['mailno'],
            'noWayBillExtend' => true,
        );
        $this->directDataProcess($result);
        return $result;
    }

    //单条查询请求
    public function searchRequest($sdf) {
        $this->title = '搜索顺丰面单_' . $this->__channelObj->channel['logistics_code'] . '获取电子面单';
        $delivery_bn =  $sdf['delivery_bn'];
        $params['orderid'] =  $delivery_bn;
        return $this->requestCall(STORE_SF_ORDERSEARCHSERVICE, $params);
    }

    #获取货物名称
    public function getCargos($deliveryItems) {
        $cargo = '';
        $cargo_count = '';
        $cargo_unit = '';
        $cargo_amount = '';
        foreach ($deliveryItems as $item) {
            $cargo .= $item['product_name'] . '/';
            $cargo_count.= $item['number'] . ',';
            $unit = '件';
            $cargo_unit .= $unit . ',';
            $amount = '100';
            $cargo_amount .= $amount . ',';
        }
        if ($cargo) {
            $cargo = trim($cargo, '/');
        }
        if ($cargo_count) {
            $cargo_count = trim($cargo_count, ',');
        }
        if ($cargo_unit) {
            $cargo_unit = trim($cargo_unit, ',');
        }
        if ($cargo_amount) {
            $cargo_amount = trim($cargo_amount, ',');
        }

        $cargos = array(
            'cargo' => $cargo,
            'cargo_count' => $cargo_count,
            'cargo_unit' => $cargo_unit,
            'cargo_amount' => $cargo_amount,
        );
        return $cargos;
    }
}